import numpy as np
from scipy.optimize import minimize

# chatgpt一顿瞎写

def reverse3(mu = 6, variance = 0.04):
    import numpy as np
    from scipy.optimize import minimize
    print(f"mean: {mu}")
    print(f"variance: {variance}")
    # 定义目标函数
    def beta_params_objective(params):
        alpha, beta = params
        calculated_mu = alpha / (alpha + beta)
        calculated_variance = (alpha * beta) / ((alpha + beta) ** 2 * (alpha + beta + 1))
        return (calculated_mu - mu) ** 2 + (calculated_variance - variance) ** 2

    # 设置初始猜测值
    initial_guess = [2, 2]

    # 设置参数边界
    bounds = [(0.1, None), (0.1, None)]

    # 通过优化找到满足均值和方差要求的Alpha和 Beta
    result = minimize(beta_params_objective, initial_guess, bounds=bounds, method='SLSQP')
    alpha, beta = result.x

    print(f"Alpha: {alpha}")
    print(f"Beta: {beta}")
    return alpha, beta

def reverse2(mu = 6, variance = 0.04):
    import numpy as np
    from scipy.optimize import minimize
    # 已知均值和方差
    
    print(f"mean: {mu}")
    print(f"variance: {variance}")

    # 定义优化函数
    def beta_params_objective(params):
        alpha, beta = params
        calculated_mu = alpha / (alpha + beta)
        calculated_variance = (alpha * beta) / ((alpha + beta) ** 2 * (alpha + beta + 1))
        return (calculated_mu - mu) ** 2 + (calculated_variance - variance) ** 2

    # 设置初始猜测值
    initial_guess = [2, 2]  # 可以根据具体情况进行调整

    # 通过优化找到满足均值和方差要求的Alpha和Beta
    result = minimize(beta_params_objective, initial_guess, method='BFGS')
    alpha, beta = result.x

    print(f"Alpha: {alpha}")
    print(f"Beta: {beta}")
    return alpha, beta

def reverse4(mu = 6, variance = 0.04):
    import numpy as np
    from scipy.optimize import fsolve
    print(f"mean: {mu}")
    print(f"variance: {variance}")
    # 定义方程
    def equations(p):
        alpha, beta = p
        eq1 = alpha / (alpha + beta) - mu
        eq2 = (alpha * beta) / ((alpha + beta)**2 * (alpha + beta + 1)) - variance
        return [eq1, eq2]

    # 初猜
    initial_guess = [2, 2]

    # 求解方程
    alpha, beta = fsolve(equations, initial_guess)

    print(f"Alpha: {alpha}")
    print(f"Beta: {beta}")
    return alpha, beta

# so上
def f():
    from scipy import optimize
    mu = 2.0
    var = 0.111111
    def f(x, mu, var):
        alpha, beta = x[0], x[1]
        return [alpha/(alpha+beta) - mu, (alpha*beta)/(((alpha+beta)**2)*(alpha+beta+1)) - var]
    rv = optimize.root(f, [1, 1], args=(mu, var)).x
    print(rv)

def zhengxiang(alpha = 2.3958970454971524, beta = 1.5972769338773674):
    # 均值 (μ) = Alpha / (Alpha + Beta)
    # 方差 (σ^2) = (Alpha * Beta) / ((Alpha + Beta)^2 * (Alpha + Beta + 1))
    # 给定Alpha和Beta参数
    print(f"正向计算")
    print(f"Alpha: {alpha}")
    print(f"Beta: {beta}")

    # 计算均值和方差
    mean = alpha / (alpha + beta)
    variance = (alpha * beta) / ((alpha + beta) ** 2 * (alpha + beta + 1))

    print(f"均值: {mean}")
    print(f"方差: {variance}")
    return mean, variance



# https://stats.stackexchange.com/questions/12232/calculating-the-parameters-of-a-beta-distribution-using-the-mean-and-variance
def getAlphaBeta0(mu, sigma):
    alpha = mu**2 * ((1 - mu) / sigma**2 - 1 / mu)
    beta = alpha * (1 / mu - 1)
    return alpha, beta

def getAlphaBeta(mu, variance):
    alpha = mu**2 * ((1 - mu) / variance - 1 / mu)
    beta = alpha * (1 / mu - 1)
    return alpha, beta

if __name__ == '__main__':
    # a,b = getAlphaBeta(2.0,	0.111111)
    zhengxiang(31.5000,	58.5000)
    # zhengxiang(-38.0, 19.0)